using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using RaidLog.Engine;

namespace RaidLog.WebClient
{
    public partial class ImportDataForm : Form
    {
        //WowDatabase _WowDatabase = null;
        ImportData.ImportDataSrv _Server = null;
        ImportData.RaidData.RaidRow _RaidID = null;

        public ImportDataForm(ImportData.ImportDataSrv server, ImportData.RaidData.RaidRow raidID)
        {
            _RaidID = raidID;
            _Server = server;
            //_WowDatabase = wowDatabase;
            InitializeComponent();
            dateTimePickerFrom.Value = raidID.StartTime;
            dateTimePickerTo.Value = raidID.EndTime;
        }

        private void buttonParse_Click(object sender, EventArgs e)
        {
            ProgressForm progressForm = new ProgressForm();
            progressForm.Show();

            new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(_Parse)).Start(progressForm);
        }

        private delegate void vvd();

        private void _Parse(object o)
        {
            ProgressForm progressForm = (ProgressForm)o;
            try
            {
                TimeSpan totalTime = dateTimePickerTo.Value.Subtract(dateTimePickerFrom.Value);
                foreach (string[] lines in Program.WowApplication.GetLineSets(dateTimePickerFrom.Value, dateTimePickerTo.Value, 500))
                {
                sendblock:
                    try
                    {
                        TimeSpan progressTime = RaidLog.Engine.WowLogFile.GetTimeFromLineText(lines[lines.Length - 1]).Value.Subtract(dateTimePickerFrom.Value);
                        _Server.AppendLogData(_RaidID.RaidID, textBoxPlayerName.Text, lines);
                        this.BeginInvoke(new vvd(delegate()
                        {
                            progressForm.Progress = progressTime.TotalSeconds / totalTime.TotalSeconds;
                        }));
                    }
                    catch (Exception ex)
                    {
                        if (MessageBox.Show(ex.ToString(), ex.Message, MessageBoxButtons.OKCancel) == DialogResult.OK)
                        {
                            goto sendblock;
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }
            finally
            {
                this.BeginInvoke(new vvd(delegate()
                {
                    progressForm.Close();
                    this.Close();
                }));
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}